package self.Lunastic.HexaSweeper.Window;

import self.Lunastic.HexaSweeper.Unit.ButtonCeil;
import self.Lunastic.HexaSweeper.Manager.GameManager;
import self.Lunastic.HexaSweeper.Object.GameArea;
import self.Lunastic.HexaSweeper.Object.GameCeil;

import java.awt.*;

/**
 * 游戏窗口
 */
public class WindowGame extends WindowBase{
    // 获取数据
    private final GameArea _areaData = GameManager.$gameArea;

    public WindowGame(int x,int y, int w, int h){
        super(x,y,w,h);
        this.setLayout(null);
        this.setOpaque(false);
    }

    public void initArea(){
        clearArea();
        createArea();
    }

    // 清空雷区
    public void clearArea(){
        for (Component c: this.getComponents()) {
            if(c instanceof ButtonCeil) {
                ((ButtonCeil) c).endUpdate(); // 给我先关了再删掉
                this.remove(c);
            }
        }
    }
    // 创建雷区
    public void createArea(){
        for (GameCeil[] i:this._areaData.getData()) {
            for (GameCeil j:i) {
                if(j.isCeil()){
                    ButtonCeil cb = new ButtonCeil(j);
                    cb.setSize(j.r*2,j.r*2);
                    cb.setLocation((2*j.x+j.y)*j.r,(int)(1.732*(j.y+0.5)*j.r));
                    this.add(cb);
                    cb.setVisible(true);
                }
            }
        }
    }

    @Override
    public void active() {
        for (Component c:this.getComponents()) {
            if(c instanceof ButtonCeil) {
                ((ButtonCeil) c).startUpdate();
                c.setEnabled(true);
            }
        }
    }

    @Override
    public void disactive() {
        for (Component c:this.getComponents()) {
            if(c instanceof ButtonCeil) {
                ((ButtonCeil) c).stopUpdate();
                c.setEnabled(false);
            }
        }
    }
}
